'''
----商超管理系统（容器版）----
项目需求：
系统需要用户先登录，再进行操作，其中包含以下功能菜单：
1.显示商品列表
2.增加商品
3.删除商品
4.设置折扣
5.修改价格
6.按价格排序（升序、降序
7.退出
  a.	使用列表嵌套字典的方式保存用户数据（包含用户名，密码，姓名）；
  b.	使用列表嵌套字典的方式保存商品数据（包含编号，名称，价格，折扣）；
  c.	编写用户登录的函数，返回登录结果；
  d.	循环提示菜单，业务完毕时返回主菜单，退出时回到登陆页面；
  e.	将功能菜单中的业务功能各自编写到函数中；
  f.	用户选择不同业务编号时，调用已经写好的各种函数。
'''
user1 = {'id': '1001', 'username': 'zhangsan', 'password': '123'}
user2 = {'id': '1002', 'username': 'lisi', 'password': '123'}
user3 = {'id': '1003', 'username': 'wangwu', 'password': '123'}
usersList=[user1,user2,user3]

product1 = {"pid": "1", "name": "苹果", "price": 1.0, "discount": 1}
product2 = {"pid": "2", "name": "香蕉", "price": 2.0, "discount": 1}
product3 = {"pid": "3", "name": "牛奶", "price": 3.0, "discount": 1}
product4 = {"pid": "4", "name": "白菜", "price": 4.0, "discount": 1}
product5 = {"pid": "5", "name": "西瓜", "price": 5.0, "discount": 1}
productsList = [product1, product2, product3, product4, product5]


def login():
    count = 0
    while True:
        tag = 0
        username = input('请输入用户名：')
        pwd = input('请输入密码：')
        for user in usersList:
            if username == user['username'] and pwd == user['password']:
                tag = 1
                print(f"登录成功！欢迎您：{user['username']}")
                break
        if tag == 0:
            count += 1
            if count == 3:
                print("用户已被锁定")
                break
            else:
                print(f"用户名或密码错误，请重新输入！还剩{3 - count}次登录机会")
                continue
        else:
            break
    return tag

def show_productes():
    print("┏--编号-----商品名称-----价格-----折扣--┓")
    for product in productsList:
        print(f"|   {product['pid']}        {product['name']}      {product['price']}       {product['discount']}   |")
    print("┗-----------------------------------┛")

def add_product():
    while True:
        pList = []
        for product in productsList:
            pList.append(int(product["pid"]))
        newId = str(max(pList) + 1)
        newName = input("请输入商品名称：")
        newPrice = float(input("请输入商品价格："))
        newDiscount = float(input("请输入商品折扣："))
        newProduct = {"pid": newId, "name": newName, "price": newPrice, "discount": newDiscount}
        productsList.append(newProduct)
        show_productes()
        print("商品：", newName, "新增成功！")
        while True:
            choice = int(input("是否继续新增商品？（1是  2否）："))
            if choice == 1:
                add_product()
            elif choice == 2:
                print("已退出新增商品操作")
                break
            else:
                print("无效操作")
                continue
        break

def del_product():
    while True:
        exist = 0
        id = input("请输入要删除的商品编号：")
        for product in productsList:
            if id == product['pid']:
                productsList.remove(product)
                print("正在删除商品：", product['name'])
                exist = 1
                break
        if exist == 0:
            print("该商品不存在")
            continue
        else:
            show_productes()
            print("删除成功，已退出删除商品操作")
            break

def set_discount():
    while True:
        exist = 0
        id = input("请输入要设置折扣的商品编号：")
        for product in productsList:
            if id == product['pid']:
                newDiscount = float(input("请输入新的折扣："))
                product['discount'] = newDiscount
                print("已成功设置商品", product['name'], "的折扣为：", newDiscount, "折")
                exist = 1
                break
        if exist == 0:
            print("该商品不存在")
            continue
        else:
            show_productes()
            print("设置成功，已退出设置折扣操作")
            break



def mod_price():
    while True:
        exist = 0
        id = input("请输入要修改价格的商品编号：")
        for product in productsList:
            if id == product['pid']:
                newPrice = float(input("请输入新的价格："))
                product['price'] = newPrice
                print("修改商品", product['name'], "的价格为：", newPrice, "元")
                exist = 1
                break
        if exist == 0:
            print("该商品不存在")
            continue
        else:
            show_productes()
            print("修改成功，已退出修改价格操作")
            break


def sort_productes():
    while True:
        choice = int(input("请选择排序方式（1升序  2逆序  3退出）："))
        if choice == 1:
            productsList.sort(key=lambda x:x['price'])
            show_productes()
        elif choice == 2:
            productsList.sort(key=lambda x:x['price'], reverse=True)
            show_productes()
        elif choice == 3:
            print("已退出排序操作")
            break
        else:
            print("输入有误，请重新输入")
            continue


def print_menu():
    print(r'----欢迎----')
    print('1.显示商品列表')
    print('2.增加商品')
    print('3.删除商品')
    print('4.设置折扣')
    print('5.修改价格')
    print('6.按价格排序（升序、降序')
    print('7.退出')



def main():
    r = login()
    if r == 1:
        while True:
            print_menu()
            choice = int(input('请输入你的选择：'))
            if choice == 1:
                show_productes()
            elif choice == 2:
                add_product()
            elif choice == 3:
                del_product()
            elif choice == 4:
                set_discount()
            elif choice == 5:
                mod_price()
            elif choice == 6:
                sort_productes()
            elif choice == 7:
                print('退出成功')
                break
            else:
                print('输入有误，请重新输入')

if __name__ == '__main__':
    main()